import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { HttpProgressEvent } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { APIBase } from 'app/modules/base/api-base';

@Injectable({
	providedIn: 'root',
})
export class ClientUpdateService extends APIBase<FormData> implements Resolve<any> {
	constructor(private http: HttpClient) {
		super(http, '/api/admin/client_update');
	}

	upload(data: FormData) {
		const req = new HttpRequest('POST', this.baseURL, data, { reportProgress: true });
		return this.http.request<HttpProgressEvent>(req).pipe(map((event: HttpProgressEvent) => event));
	}

	publish() {
		return this.http.post(this.baseURL + '/pub', null).toPromise();
	}

	resolve(route: ActivatedRouteSnapshot) {
		return this.list();
	}
}
